﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;


namespace DAL
{
    public class GetAccessTokenDAL
    {
#pragma warning disable CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        string Conn = ConfigurationSettings.AppSettings["con"].ToString();
#pragma warning restore CS0618 // “ConfigurationSettings.AppSettings”已过时:“This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings”
        /// <summary>
        /// 从数据库的存储过程中获取accesstoken
        /// </summary>
        /// <returns></returns>
        public string getAccesstoken() 
        {
            SqlConnection con = new SqlConnection(Conn);
            SqlCommand cmd = new SqlCommand("verifytoken",con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            con.Open();
            string accesstoken = cmd.ExecuteScalar().ToString();
            con.Close();
            return accesstoken;
        }

        //更新存储access_token
        public int updateAccesstoken(string expires_in,string accesstoken)
        {
            SqlParameter[] sp = new SqlParameter[] { 
                new SqlParameter("@expires_in",expires_in),
                new SqlParameter("@accesstoken",accesstoken)
            };
            string sqlstr = "update access_token set expires_in = @expires_in,accesstoken = @accesstoken,createtime = getdate() where id = 1";
            int i = SqlHelper.ExecuteNonQuery(Conn,CommandType.Text,sqlstr,sp);
            return i;
        }

        ////测试数据库获取menu
        //public DataTable getMenu()
        //{
        //    string sqlstr = "select * from menu1";
        //    SqlDataAdapter sda = new SqlDataAdapter(sqlstr, Conn);
        //    DataTable dt = new DataTable();
        //    sda.Fill(dt);
        //    return dt;
        //}
    }
}
